package com.blog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.entity.Comment;
import com.blog.entity.User;
import com.blog.service.CommentService;
import com.blog.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comments")
@CrossOrigin
public class CommentController {

    private final CommentService commentService;
    private final UserService userService;

    public CommentController(CommentService commentService, UserService userService) {
        this.commentService = commentService;
        this.userService = userService;
    }

    @PostMapping
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> createComment(@RequestBody Comment comment) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.findByUsername(username);
        comment.setUserId(user.getId());
        return ResponseEntity.ok(commentService.createComment(comment));
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> updateComment(@PathVariable Long id, @RequestBody Comment comment) {
        comment.setId(id);
        return ResponseEntity.ok(commentService.updateComment(comment));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getComment(@PathVariable Long id) {
        return ResponseEntity.ok(commentService.findById(id));
    }

    @GetMapping("/article/{articleId}")
    public ResponseEntity<?> getCommentsByArticle(
            @PathVariable Long articleId,
            @RequestParam(required = false) Integer current,
            @RequestParam(required = false) Integer size) {
        if (current != null && size != null) {
            Page<Comment> page = new Page<>(current, size);
            IPage<Comment> comments = commentService.findByArticleId(articleId, page);
            return ResponseEntity.ok(comments);
        } else {
            List<Comment> comments = commentService.findByArticleId(articleId);
            return ResponseEntity.ok(comments);
        }
    }

    @GetMapping("/user/{userId}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> getCommentsByUser(
            @PathVariable Long userId,
            @RequestParam(required = false) Integer current,
            @RequestParam(required = false) Integer size) {
        if (current != null && size != null) {
            Page<Comment> page = new Page<>(current, size);
            IPage<Comment> comments = commentService.findByUserId(userId, page);
            return ResponseEntity.ok(comments);
        } else {
            List<Comment> comments = commentService.findByUserId(userId);
            return ResponseEntity.ok(comments);
        }
    }
} 